package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.LoginHistoryDAO;
import com.xinghou.platform.model.main.LoginHistory;
/**
 * @author liuwei.wen
 *
 */
@Service
public class LoginHistoryDAOImpl {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public LoginHistory getLoginHistoryById(Integer Id)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select Id,User_id,Longitude,Latitude,Device_token,Login_time from login_history where Id = ?";
		LoginHistory loginHistory = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new LoginHistoryMapper());
		CustomerContextHolder.clearCustomerType();
		return loginHistory;
	}

	@Override
	public int saveLoginHistory(LoginHistory loginHistory)
			throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO login_history(User_id,Longitude,Latitude,Device_token,Login_time)values(?,?,?,?,?)"
				);
		Object[] params = {loginHistory.getUserId(), loginHistory.getLongitude(),loginHistory.getLatitude(),loginHistory.getDeviceToken(), loginHistory.getLoginTime()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret=jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	private static final class LoginHistoryMapper implements RowMapper<LoginHistory> {
		public LoginHistory mapRow(ResultSet rs, int rowNum) throws SQLException {
			LoginHistory loginHistory = new LoginHistory();
			loginHistory.setUserId(rs.getInt("User_id"));
			//loginHistory.setLoginTime(rs.getTimestamp("Login_time"));
			loginHistory.setDeviceToken(rs.getString("Device_token"));
			loginHistory.setLongitude(rs.getDouble("Longitude"));
			loginHistory.setLatitude(rs.getDouble("Latitude"));
			return loginHistory;
		}
	}*/

}
